<?php
class Nilai_siswa_raport extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->helper('format');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
		$this->load->model('penilaian_model');
	}
	
	function index($act='')
	{
		$data['value_tingkat'] = '';
		$data['value_kelas'] = '';
		$data['value_namaanggota'] = '';
		$data['nip_guru'] = '';
		if($act == 'act_filter'){
			$data['value_tingkat'] = $this->input->post('tingkat');
			$data['value_kelas'] = $this->input->post('kelas');
			$data['value_namaanggota'] = $this->input->post('nama_anggota');
			$data['nip_guru'] = $this->input->post('nisn');
			$this->session->set_userdata('sess_nilai_tingkat', $this->input->post('tingkat'));
			$this->session->set_userdata('sess_nilai_kelas', $this->input->post('kelas'));
			$this->session->set_userdata('sess_nilai_namaanggota', $this->input->post('nama_anggota'));
			$this->session->set_userdata('sess_nilai_nipguru', $this->input->post('nisn'));
		}else{
			$data['value_tingkat'] = $this->session->userdata('sess_nilai_tingkat');
			$data['value_kelas'] = $this->session->userdata('sess_nilai_kelas');
			$data['value_namaanggota'] = $this->session->userdata('sess_nilai_namaanggota');
			$data['nip_guru'] = $this->session->userdata('sess_nilai_nipguru');
		}
	
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		// $colModel['no'] = array('No',40,TRUE,'center',0);
		if($this->session->userdata('roleid')=='0') //diknas
			$colModel['NAMA_SEKOLAH'] = array('Nama Sekolah',100,TRUE,'center',1);
		$colModel['TAHUN_AJARAN'] = array('Tahun Ajaran',70,TRUE,'center',1);
		$colModel['TINGKAT'] = array('Tingkat',50,TRUE,'center',1);
		$colModel['KELAS'] = array('Kelas',50,TRUE,'center',1);
		$colModel['SEMESTER'] = array('Semester',50,TRUE,'center',1);
		$colModel['NIP'] = array('NIP',70,TRUE,'center',1);
		$colModel['NAMA_LENGKAP'] = array('Nama Guru',300,TRUE,'center',1);
		$colModel['NAMA_MATA_PELAJARAN'] = array('Mata Pelajaran',200,TRUE,'center',0);
		// $colModel['ASPEK_PENILAIAN'] = array('Aspek Penilaian',200,TRUE,'center',0);
		$colModel['nilai_siswa'] = array('Input Nilai Siswa',80,FALSE,'center',0);
		$colModel['nilai_raport'] = array('Input Nilai Raport',80,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Mata Pelajaran Guru'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
		// if($data['value_semester']!=null and $data['value_tingkat']!=null and $data['value_tingkat']!=null)
			// $buttons[] = array('Add','add','org_js');
		// $buttons[] = array('Delete','delete','org_js');
		// $buttons[] = array('separator');
		// $buttons[] = array('Select All','add','org_js');
		// $buttons[] = array('Deselect All','delete','org_js');
		$buttons[] = array('separator');
		// $buttons[] = array('Back to Pelajaran','back','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/nilai_siswa_raport/listnilaisiswa_grid/".$this->session->userdata('unitid')."/".$data['value_tingkat']."/".$data['value_kelas']."/".$this->session->userdata('semester')."/".$data['nip_guru']),$colModel,'no','asc',$gridParams,$buttons);
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] = '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Add')
				{
					 location.href='".base_url()."index.php/nilai_siswa/input_nilaisiswa/".$this->session->userdata('unitid')."/".$data['value_tingkat']."/".$data['value_kelas']."/".$this->session->userdata('semester')."'; 
				}
				
				if (com=='Delete')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/nilai_siswa/delete_nilaisiswa')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			} 
			</script>
			";
			
		$data['tingkat'] = $this->master_model->combo_tingkat_kelas($this->session->userdata('unitid'));
		$data['kelas'] = $this->master_model->combo_kelas($this->session->userdata('unitid'),$this->session->userdata('tahunajaran'),$data['value_tingkat']);
		// $data['semester'] = $this->pelajaran_model->getSmt($this->session->userdata('unitid'));
		$data['pengajar'] = $this->penilaian_model->getGuru();
		
		$data['url_filter'] = 'nilai_siswa_raport/index/act_filter';
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listnilaisiswa_grid($id_sekolah, $id_tingkat='',$id_kelas='',$id_smt='',$nip_guru='')
	{	
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		// if($this->session->userdata('roleid')=='0') //diknas
			// $valid_fields = array('NAMA_SEKOLAH','NAMA_TA','KODE_MATA_PELAJARAN','NAMA_MATA_PELAJARAN','KET');
		// else	
			$valid_fields = array('NAMA_TA','NAMA_MATA_PELAJARAN','ASPEK_PENILAIAN');
		
		$this->flexigrid->validate_post('p.KODE_MATA_PELAJARAN','asc',$valid_fields);

		$records = $this->penilaian_model->get_nilai_siswa($id_sekolah,$id_tingkat,$id_kelas,$id_smt,$nip_guru);
		
		// var_dump($records);
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		$pembanding = array();$pembanding[0]='';$i=1;
		foreach ($records['records']->result() as $row)
		{
			$pembanding[$i] = $row->KODE_MATA_PELAJARAN;
			if($this->session->userdata('roleid')=='0'){ //diknas
				$record_items[] = array($row->ID_SEK."#".$row->ID_TA."#".$row->ID_TINGKAT."#".$row->ID_KELAS."#".$row->ID_SMT."#".$row->TANGGAL,
				// $no+1,
				'<div align="left">'.$row->NAMA_TA.'</div>',
				'<div align="left">'.$row->TINGKAT.'</div>',
				'<div align="left">'.$row->KELAS.'</div>',
				'<div align="left">'.$row->SEMESTER.'</div>',
				'<div align="left">'.$row->NAMA_LENGKAP.'</div>',
				'<div align="left">'.$row->NAMA_MATA_PELAJARAN.'</div>',
				'<div align="left">'.$row->KODE_RPP.'</div>',
				'<div align="left">'.$row->MATERI.'</div>',
				// '<div align="left">'.$row->DESKRIPSI.'</div>',
				'<a title="Input Pengajar pada Mata Pelajaran ini." href=\''.site_url().'/ms_pelajaran_guru/index/'.$row->KODE_MATA_PELAJARAN.'/'.$row->ID_SEKOLAH.'/'.$row->TAHUN_AJARAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/39.png\'></a> ',
				);
				$no++;
			}else{
				if($pembanding[$i-1]!=$pembanding[$i]){
					$record_items[] = array($row->ID_PG,
					'<div align="left">'.$row->NAMA_TA.'</div>',
					'<div align="left">'.$row->TINGKAT.'</div>',
					'<div align="left">'.$row->KELAS.'</div>',
					'<div align="left">'.$row->SEMESTER.'</div>',
					'<div align="left">'.$row->NIP.'</div>',
					'<div align="left">'.$row->NAMA_LENGKAP.'</div>',
					'<div align="left">'.$row->NAMA_MATA_PELAJARAN.'</div>',
					'<a title="Input Nilai Siswa Untuk Pelajaran ini." href=\''.site_url().'/nilai_siswa/index/'.$row->ID_SEK.'/'.$row->ID_TA.'/'.$row->ID_TINGKAT.'/'.$row->ID_KELAS.'/'.$row->ID_SMT.'/'.$row->KODE_MATA_PELAJARAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/39.png\'></a> ',
					'<a title="Input Nilai Raport." href=\''.site_url().'/nilai_raport/index/'.$row->ID_SEK.'/'.$row->ID_TA.'/'.$row->ID_TINGKAT.'/'.$row->ID_KELAS.'/'.$row->ID_SMT.'/'.$row->KODE_MATA_PELAJARAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/39.png\'></a> '
					);
				}else{
					$record_items[] = array($row->ID_PG,
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>',
					'<div align="left"></div>'
					// '<a title="Input Nilai Siswa Untuk Pelajaran ini." href=\''.site_url().'/nilai_siswa/index/'.$row->ID_SEK.'/'.$row->ID_TA.'/'.$row->ID_TINGKAT.'/'.$row->ID_KELAS.'/'.$row->ID_SMT.'/'.$row->KODE_MATA_PELAJARAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/39.png\'></a> ',
					// '<a title="Input Nilai Raport." href=\''.site_url().'/nilai_raport/index/'.$row->ID_SEK.'/'.$row->ID_TA.'/'.$row->ID_TINGKAT.'/'.$row->ID_KELAS.'/'.$row->ID_SMT.'/'.$row->KODE_MATA_PELAJARAN.'/\'><img border=\'0\' src=\''.base_url().'images/main/39.png\'></a> '
					);
				}
			}
		$i++;}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
}
?>
